package io.geekidea.boot.system.controller;

import com.google.common.base.Joiner;
import com.hwtx.form.controller.Api2Result;
import com.hwtx.form.domain.def.ResourceTreeOption;
import com.hwtx.form.domain.service.ResourceService;
import io.geekidea.boot.auth.annotation.Permission;
import io.geekidea.boot.system.query.SysMenuQuery;
import io.geekidea.boot.system.vo.AmisTableData;
import io.geekidea.boot.system.vo.OptionsData;
import io.geekidea.boot.system.vo.PermissionVo;
import io.geekidea.boot.system.vo.SysMenuTreeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.hwtx.form.domain.FormConstants.AUTH_KEY;
import static com.hwtx.form.domain.FormConstants.LIST_URL_KEY;

/**
 * 系统菜单 控制器
 *
 * @author geekidea
 * @since 2022-12-26
 */
@Slf4j
@RestController
@Tag(name = "资源管理")
@RequestMapping("/admin/resource")
public class SysMenuController {

    @Resource
    private ResourceService resourceService;

    /**
     * 删除系统菜单
     */
    @PostMapping("/deleteSysMenu/{id}")
    @Operation(summary = "删除系统菜单")
    public Api2Result deleteSysMenu(@PathVariable Long id) {
        boolean flag = resourceService.deleteSysMenu(id);
        return Api2Result.result(flag);
    }

    /**
     * 获取所有的系统菜单树形列表
     */
    @RequestMapping("/getAllSysMenuTreeList")
    @Operation(summary = "获取所有的系统菜单树形列表")
    public Api2Result getAllSysMenuTreeList(@Valid @RequestBody SysMenuQuery query) {
        List<SysMenuTreeVo> list = resourceService.getResourceTree(query);
        AmisTableData<SysMenuTreeVo> misTableData = new AmisTableData<>();
        misTableData.setRows(list);
        return Api2Result.success(misTableData);
    }

    @RequestMapping("/getPermissions")
    @Operation(summary = "获取菜单下的权限配置", extensions = {
            @Extension(properties = {
                    @ExtensionProperty(name = AUTH_KEY, value = LIST_URL_KEY),
            })})
    public Api2Result getPermissions(@Valid @RequestBody SysMenuQuery query) {
        query.setStatus(true);
        List<PermissionVo> list = resourceService.getPermissions(query);
        AmisTableData<PermissionVo> misTableData = new AmisTableData<>();
        misTableData.setRows(list);
        return Api2Result.success(misTableData);
    }

    @GetMapping("/menus")
    @Operation(summary = "获取所有的系统菜单树形列表")
    public Api2Result getMenus() {
        return Api2Result.success(resourceService.getResources());
    }

    @PostMapping("/resourceForRole")
    @Operation(summary = "获取角色拥有的资源")
    public Api2Result getResourceForRole(@Valid @RequestBody SysMenuQuery query) {
        List<ResourceTreeOption> list = resourceService.getResourceForRoleTree(query);
        List<Long> resourceId = resourceService.getMenuIdsByRoleId(query.getRoleId(), null);
        OptionsData<ResourceTreeOption> misTableData = new OptionsData<>();
        misTableData.setOptions(list);
        misTableData.setValue(Joiner.on(',').join(resourceId));
        return Api2Result.success(misTableData);
    }
}
